package com.qianfeng.zookeeper.zookeeper.watcher;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

/**
 * Created by jackiechan on 2021/7/9 11:32
 *
 * @author jackiechan
 * 请记住 2021-06-24 这个让我变成 SB 的日子
 */

public class ZookeeperWatcher {
    private static final String ZKADDR = "10.9.12.200:8601,10.9.12.200:8602,10.9.12.200:8603";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);//用作阻塞的, 1 的意思是只需有一个地方执行了放行就放行所有阻塞的地方
    private static ZooKeeper zooKeeper = null;

    public static void main(String[] args) throws Exception {
        zooKeeper = new ZooKeeper(ZKADDR, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                Event.KeeperState state = event.getState();
                System.err.println("getState===>" + state);
                String path = event.getPath();
                System.err.println("getPath===>" + path);
                Event.EventType type = event.getType();
                System.err.println("getType===>" + type);

                switch (state) {
                    case SyncConnected:
                        if (type == Event.EventType.NodeCreated) {
                            try {
                                byte[] data = zooKeeper.getData(path, false, null);
                                System.err.println(path + "创建了,传递的内容是:" + new String(data));
                            } catch (KeeperException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        break;
                }
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        //监听要先监听后执行相关操作才可以
        //   Stat stat = zooKeeper.exists("/aaaaaaaaa12", true);//判断某个节点是否存在,因为有 watcher 所以设置为 true
        //  System.err.println(stat);
        //  String s = zooKeeper.create("/aaaaaaaaa12", "dasdasdasd".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //   System.err.println(s);
        //countDownLatch.await();

        //要想获取数据发生变化,就必须先监听后修改
        byte[] data = zooKeeper.getData("/aaaaaaaaa12", true, null);
        System.err.println(new String(data));
        zooKeeper.setData("/aaaaaaaaa12", "afsfdsf".getBytes(StandardCharsets.UTF_8), -1);
        System.in.read();

    }
}
